/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.expression.dcExpression;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.gui.NumberDragSlider;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty.Flag;
import tal.drivechain.variable.VarTableAction;
import tal.drivechain.variable.Variable;

/**
 *
 * @author eisental
 */
public class NumberBox extends GUIModule implements ActionListener {
    private VarMatcher trigger, releaseTrigger;
    private NumberDragSlider slider;
    private Variable varValue = new Variable("value", DataType.FLOAT, null, true), onRelease;
    private VarTableAction setVarValue;
    private boolean releaseOnChange;
    private double initValue;

    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        initValue = (Double)props.getProperty("initValue");
        component = slider = new NumberDragSlider(initValue);
        super.setupProperties();
        slider.addActionListener(this);
        setVarValue = new VarTableAction("value", VarTableAction.Action.SET_ANY);
        varValue.setValue(initValue, true);
    }

    public Object getInitValue() { return initValue; }

    @Override
    public void setColor(Color value) {
        super.setColor(value);
        slider.setColor(color);
    }

    @Override
    public void setEnabled(Boolean value) {
        super.setEnabled(value);
        slider.setEnabled(enabled);
    }

    @Override
    public void setFont(Font value) {
        super.setFont(value);
        slider.setFont(font);
        relayout();
    }

    @Override
    public void setNegative(Boolean value) {
        super.setNegative(value);
        slider.setNegative(negative);
    }

    public void setReleaseOnChange(Boolean value) {
        releaseOnChange = value;
    } public Boolean getReleaseOnChange() { return releaseOnChange; }

    public void setTrigger(VarMatcher value) {
        trigger = value;
    } public VarMatcher getTrigger() { return trigger; }

    public void setReleaseTrigger(VarMatcher value) {
        releaseTrigger = value;
    } public Object getReleaseTrigger() { return releaseTrigger; }

    public void setMinValue(Double value) {
        slider.setMin(value);
    } public double getMinValue() { return slider.getMin(); }

    public void setMaxValue(Double value) {
        slider.setMax(value);
    } public double getMaxValue() { return slider.getMax(); }

    public void setDragScale(Double value) {
        slider.setScale(value);
    } public double getDragScale() { return slider.getScale(); }

    public void setColumns(Integer value) {
        slider.setColumns(value);
        relayout();
    } public double getColumns() { return slider.getColumns(); }

    public void setOnRelease(Variable value) {
        onRelease = value;
    } public Variable getOnRelease() { return onRelease; }

    public void setDisplay(dcExpression value) {
        slider.setDisplay(value);
    } public dcExpression getDisplay() { return slider.getDisplay(); }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (trigger.matches(in, channel, this)) {
            try {
                slider.setValue((Double)DataType.FLOAT.cast(in.getValue(this)));
            } catch (IllegalArgumentException ie) {
                log("Invalid matching incoming variable: " + in + " (" + ie.getMessage() + ").", MSG.WARNING);
            }
        } if (releaseTrigger.matches(in, channel, this)) {
            release();
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==slider) {
            if (releaseOnChange)
                release();
        }
    }

    private void release() {
        varValue.setValue(slider.getValue(), true);
        setVarValue.set(varValue, localVars, this);
        sendToOutputs(onRelease, -1);
    }
}
